package com.vn.gofriends.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.parse.ParseACL;
import com.parse.ParseInstallation;
import com.parse.ParseObject;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.vn.gofriends.R;
import com.vn.gofriends.ui.MyEditTextDatePicker;

public class CreateMyTripActivity extends Activity {
	// Button btnCreate;
	
	EditText edtTitle;
	EditText edtInit;
	EditText edtEnd;
	EditText edtDestination;
	EditText edtDescription;
	CheckBox ckIsPrivate;
	String titleTrips, addressTrips,descriptionTrips;
	boolean isPublic;
	String init, ends;
	Date dateInit,dateEnd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_trips_layout);

		ActionBar mActionBar = getActionBar();
		mActionBar.setDisplayShowHomeEnabled(false);
		mActionBar.setDisplayShowTitleEnabled(false);
		LayoutInflater mInflater = LayoutInflater.from(this);
		View mCustomView = mInflater.inflate(R.layout.actionbar_create_trip,
				null);
		mCustomView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		mCustomView.findViewById(R.id.btnDiscard).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						finish();
					}
				});
		mCustomView.findViewById(R.id.btnDone).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						titleTrips = edtTitle.getText().toString();
						Log.d("Vien", "title: "+ titleTrips);
						addressTrips = edtDestination.getText().toString().trim();
						descriptionTrips = edtDescription.getText().toString().trim();
						init = (edtInit.getText().toString().trim());
						ends = (edtEnd.getText().toString().trim());
						final ParseObject trips = new ParseObject("Trip");
						final ParseObject traveler = new ParseObject("Traveler");
						trips.put("title", titleTrips);
						trips.put("description", descriptionTrips);
						ParseACL acl = new ParseACL();
						acl.setPublicReadAccess(isPublic);
						trips.setACL(acl);
						SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
						dateInit = new Date();
						dateEnd = new Date();
					    try {
					        dateInit = dateFormat.parse(init);
					        dateEnd = dateFormat.parse(ends);
					    } catch (ParseException e) {
					        // TODO Auto-generated catch block
					        e.printStackTrace();
					    }
						trips.put("dateInit", dateInit );
						trips.put("dateEnd", dateEnd);
						trips.put("idOwner", ParseUser.getCurrentUser());
						trips.put("isPublic" , isPublic);
						traveler.put("idUser", ParseUser.getCurrentUser());
						
						trips.saveInBackground(new SaveCallback() {
							
							@Override
							public void done(com.parse.ParseException e) {
								// TODO Auto-generated method stub
								if(e ==null){
									if(isPublic){
										ParsePush push = new ParsePush();
										// Create our Installation query
										ParseQuery pushQuery = ParseInstallation.getQuery();
										push.setQuery(pushQuery); // Set our Installation query
										push.setMessage("Co chuyen du lich moi nek moi nguoi. Nhanh tay dang ky nao. ^_^");
										push.sendInBackground();
									}
									traveler.put("idTrip", trips);
									traveler.saveInBackground();
									setResult(RESULT_OK);
									Toast.makeText(getApplicationContext(), "Add success", Toast.LENGTH_SHORT).show();
									finish();
								}
							}
						});
						
					}
				});
		
		mActionBar.setCustomView(mCustomView);
		mActionBar.setDisplayShowCustomEnabled(true);
		edtTitle = (EditText) findViewById(R.id.edtTitle);
		edtDescription = (EditText) findViewById(R.id.edtDescription);
		edtDestination = (EditText) findViewById(R.id.edtDestination);
		edtInit = (EditText) findViewById(R.id.edtStart);
		edtEnd = (EditText) findViewById(R.id.edtEnds);
		ckIsPrivate = (CheckBox) findViewById(R.id.ckPublic);
		edtDestination.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(CreateMyTripActivity.this,
						DestinationActivity.class));
			}
		});
		
		Bundle b = new Bundle();
		b = getIntent().getExtras();
		if (b != null) {
			String nameCity = b.getString("nameCity");
			edtDestination.setText(nameCity);
		}
		MyEditTextDatePicker datePickerStart = new MyEditTextDatePicker(
				CreateMyTripActivity.this, edtInit);
		MyEditTextDatePicker datePickerEnd = new MyEditTextDatePicker(
				CreateMyTripActivity.this, edtEnd);
		ckIsPrivate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (((CheckBox) v).isChecked()) {
					isPublic = true;
				}
				else {
					isPublic = false;
				}
		
			}
		});
	}
}
